var datasourceManager = {
    searchUrl: "/generate/hzbgdatasource/pageFindAll",
    saveUrl: "/generate/hzbgdatasource/save",
    editUrl: "/generate/hzbgdatasource/edit",
    removeUrl: "/generate/hzbgdatasource/remove",
    datasourceInfoLayer: null,
    isAdd: false,
    datasourceTable: null,
    init: function () {
        layui.use(['table', 'util', 'layer', 'form'], function () {
            datasourceManager.refreshTable();
            hs_utils.renderDictionarySelect('sourceType', 'datasourceForm', '8db2dfe6-2e32-11eb-a232-00163e106792', 'strValue');
            layui.form.on('submit(datasourceSearch)', datasourceManager.search);
        });
    },

    // 刷新表格
    refreshTable: function () {
        datasourceManager.datasourceTable = hs_utils.renderPageTable(datasourceManager.tableConfig(), datasourceManager.eventConfig());
    },

    // 打开数据源信息窗口
    openFormLayer: function () {
        datasourceManager.datasourceInfoLayer = layui.layer.open({
            type: 1,
            title: '数据源信息',
            area: ["650px", "580px"],
            content: $("#datasourceDiv")
        });
        layui.form.on('submit(datasourceSave)', datasourceManager.submit)
    }
    ,

    // 提交数据
    submit: function (data) {
        let url_ = datasourceManager.editUrl;
        if (datasourceManager.isAdd) {
            url_ = datasourceManager.saveUrl;
        }
        hs_utils.post(url_, data.field, function (data) {
            if (data.success) {
                layui.layer.close(datasourceManager.datasourceInfoLayer);
                layui.layer.msg("操作成功！");
                datasourceManager.reloadTable();
            } else {
                layui.layer.msg(data.msg);
            }
        });
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    }
    ,

    // 删除
    remove: function (hbgdsId) {
        hs_utils.post(datasourceManager.removeUrl, {hbgdsIds: hbgdsId}, function (data) {
            if (data.success) {
                layui.layer.msg("操作成功！");
                datasourceManager.reloadTable();
            } else {
                layui.layer.msg(data.msg);
            }
        });
    },

    // 查询
    search: function (data) {
        datasourceManager.reloadTable(data.field);
        return false;
    },

    // 重载表格
    reloadTable: function (data) {
        if (data) {
            datasourceManager.datasourceTable.reload({
                where: data
            });
        } else {
            datasourceManager.datasourceTable.reload();
        }
    },

    // 表格参数
    tableConfig: function () {
        return {
            elem: '#datasourceTable',
            url: datasourceManager.searchUrl, //数据接口
            toolbar: '#datasourceTableToolBar',
            cols: [[ //表头
                {field: 'hbgdsName', title: '数据源名称', align: "center"},
                {field: 'sourceType',title: '数据源类型',align: "center"},
                {field: 'sourceDbname', title: '库名', align: "center"},
                {
                    field: 'hbgdsId', title: '操作', align: "center",
                    toolbar: "#datasourceTableItemToolBar"
                }
            ]]
        };
    },

    // 表格事件参数
    eventConfig: function () {
        return {
            filter: 'datasourceTable',
            toolbar: [
                {
                    name: 'add', handler: function (obj) {
                        $('#datasourceForm')[0].reset();
                        datasourceManager.isAdd = true;
                        datasourceManager.openFormLayer();
                    }
                }
            ],
            tool: [
                {
                    name: 'remove', handler: function (obj) {
                        layui.layer.confirm('真的删除行么', function (index) {
                            layui.layer.close(index);
                            //向服务端发送删除指令
                            datasourceManager.remove(obj.data.hbgdsId);
                        });
                    }
                },
                {
                    name: 'edit', handler: function (obj) {
                        layui.form.val('datasourceForm', obj.data);
                        datasourceManager.isAdd = false;
                        datasourceManager.openFormLayer();
                    }
                }
            ]
        };
    }
}
datasourceManager.init();